﻿using Infrastructure.ViewModel;
using UltimateNavigationFramework.UriNavigation;
using Microsoft.Practices.Prism.Regions;
using System.Windows;

namespace Ultimate.Home.ViewModels
{
    public class HomeSubInnerViewModel : ViewModelBase, IPageTitle, IConfirmNavigationRequest
    {
        private string _id;
        public string Id
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
                this.NotifyPropertyChanged(() => this.Id);
            }
        }

        #region IPageTitle Implementation
        public string PageTitle
        {
            get { return "Home Sub Inner View"; }
        } 
        #endregion

        public void ConfirmNavigationRequest(NavigationContext navigationContext, System.Action<bool> continuationCallback)
        {
            // Because it is nested, we need to exclude the nested levels and wont be called by itself so it will only be called once
            // When called from Journal GoBack/Forward, it will not contain a 'View', so we want to ignore that as well
            // This is the convention used in the demo, otherwise, come up with your own.
            if (!navigationContext.Uri.OriginalString.Contains("HomeSubView") 
                && !navigationContext.Uri.OriginalString.Contains("HomeSubInnerView") 
                && navigationContext.Uri.OriginalString.Contains("View"))
            {
                // Note: This is not mvvm friendly, too lazy to implement Interactionservice from prism
                var result = MessageBox.Show("Confirm Navigate from Inner View?", "Confirmation", MessageBoxButton.OKCancel);
                if (result == MessageBoxResult.OK)
                    continuationCallback(false); // Will navigate away
                else
                    continuationCallback(true); // Will not navigate away
            }
            else
                continuationCallback(false);
        }

        bool INavigationAware.IsNavigationTarget(NavigationContext navigationContext)
        {
            // Will load each instance based on the unique id
            return this.Id == navigationContext.Parameters["id"];               
        }

        void INavigationAware.OnNavigatedFrom(NavigationContext navigationContext)
        {
            
        }

        void INavigationAware.OnNavigatedTo(NavigationContext navigationContext)
        {
            var id = navigationContext.Parameters["id"];
            this.Id = id;
        }
    }
}
